package com.itchu.customer;

import com.itchu.common.domain.PageResult;
import com.itchu.system.domain.Category;
import com.itchu.system.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/category")
public class vCategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 查找分类的分页信息
     * @param key   搜索条件
     * @param page  当前页
     * @param rows  页大小
     * @param sortBy
     * @param desc
     * @return
     */
    @GetMapping("/page")
    public ResponseEntity<PageResult<Category>> findByPage(
            @RequestParam(value = "key", required = false) String key,
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "5") Integer rows,
            @RequestParam(value = "sortBy", required = false) String sortBy,
            @RequestParam(value = "desc", required = false) Boolean desc
    ) {
        PageResult<Category> list = categoryService.findByPage(key,page,rows,sortBy,desc);
        return ResponseEntity.ok(list);
    }

    /**
     * 根据分类的父id查找出所有父级分类
     * @param pid
     * @return
     */
    @GetMapping("/of/parent")
    public ResponseEntity<List<Category>> showCategoryByPid(
            @RequestParam("pid") Long pid
    ) {
        List<Category> list = categoryService.showCategoryByPid(pid);
        return ResponseEntity.ok(list);
    }

}
